using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Umbraco.Ecommerce.BusinessLogic;
using System.Drawing;


namespace Umbraco.Ecommerce.WebShop.UserControls
{
    public partial class Cart : System.Web.UI.UserControl
    {
        private string _checkouturl = "";
        public string OptionalCheckOutUrl
        {
            get
            {
                return "";
            }
            set
            {
                _checkouturl = value;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)

                BindData();
        }
        private void BindData()
        {
            if (Library.GetCart.Items.Length > 0)
            {

                NoItems.Visible = false;
                cartholder.Visible = true;

                //set Remove label
                if (umbraco.library.GetDictionaryItem("remove") != "")
                    lttRemove.Text = umbraco.library.GetDictionaryItem("remove");
                //set Description label
                if (umbraco.library.GetDictionaryItem("description") != "")
                    lttDescription.Text = umbraco.library.GetDictionaryItem("description");
                //set Quantity label          
                if (umbraco.library.GetDictionaryItem("quantity") != "")
                    lttQuantity.Text = umbraco.library.GetDictionaryItem("quantity");
                //set Total label
                if (umbraco.library.GetDictionaryItem("total") != "")
                    lttHeadTotal.Text = umbraco.library.GetDictionaryItem("total");
                lttTotal2.Text = umbraco.library.GetDictionaryItem("total");
                //set subTotal label
                if (umbraco.library.GetDictionaryItem("subtotal") != "")
                    lttSubtotal2.Text = umbraco.library.GetDictionaryItem("subtotal");
                //set Shipping label
                if (umbraco.library.GetDictionaryItem("shipping") != "")
                    lttShipping2.Text = umbraco.library.GetDictionaryItem("shipping");
                //set Vat label
                if (umbraco.library.GetDictionaryItem("vat") != "")
                    lttVat2.Text = umbraco.library.GetDictionaryItem("vat");
                //set update button
                if (umbraco.library.GetDictionaryItem("updatecart") != "")
                    UpdateCart.Text = umbraco.library.GetDictionaryItem("updatecart");
                //set Continue button
                if (umbraco.library.GetDictionaryItem("continueshopping") != "")
                    Continue.Text = umbraco.library.GetDictionaryItem("continueshopping");
                //set Checkout button
                if (umbraco.library.GetDictionaryItem("Checkout") != "")
                    Checkout.Text = umbraco.library.GetDictionaryItem("Checkout");

                cartItemRepeater.DataSource = Library.GetCart.Items;
                cartItemRepeater.DataBind();


                decimal d = 1 + (decimal.Parse(BusinessLogic.Store.vat.ToString()) / 100);
                decimal totalprice = Library.GetCart.TotalSum + Store.GetDeliveryCostByCountry(Store.GetDeliveryCountry);
                decimal priceExVat = (totalprice / d);
                decimal vat = (totalprice - priceExVat);

                lttSubTotal.Text = Library.FormattedPrice(Library.GetCart.TotalSum);
                lttShipping.Text = Library.FormattedPrice(Store.GetDeliveryCostByCountry(Store.GetDeliveryCountry));
                lttVat.Text = Library.FormattedPrice(vat);
                lttTotal.Text = Library.FormattedPrice(Store.GetDeliveryCostByCountry(Store.GetDeliveryCountry) + Library.GetCart.TotalSum);


            }
            else
            {
                cartholder.Visible = false;
                NoItems.Visible = true;
            }
        }

        protected void UpdateCart_Click(object sender, EventArgs e)
        {
            // Update the cart
            int max = cartItemRepeater.Items.Count - 1;

            for (int i = max; i >= 0; i--)
            {
                RepeaterItem rpi = cartItemRepeater.Items[i];
                CheckBox ckb = (CheckBox)rpi.FindControl("ckbRemove");
                // Get productid
                string ProdId = ((System.Web.UI.HtmlControls.HtmlInputHidden)rpi.FindControl("productid")).Value;

                if (ckb.Checked)
                {
                    // Remove item from cart
                    Library.GetCart.RemoveItem(ProdId);
                }
                else
                {
                    // Update the cart
                    int Quantity = int.Parse(((DropDownList)rpi.FindControl("ddlQuantity")).SelectedValue);
                    Library.GetCart.SetQuantityOnItem(ProdId, Quantity);
                }
            }

            // Rebind updated data.
            BindData();
        }

        protected void Continue_Click(object sender, EventArgs e)
        {
            Response.Redirect(umbraco.library.NiceUrl(umbraco.presentation.nodeFactory.Node.GetCurrent().Id));

        }


        protected void Checkout_Click(object sender, EventArgs e)
        {
            if (_checkouturl != "")
                Response.Redirect(_checkouturl);

            // Go to checkout
            string currenturl = umbraco.library.NiceUrl(umbraco.presentation.nodeFactory.Node.GetCurrent().Id);
            string checkouturl;
            if (umbraco.GlobalSettings.UseDirectoryUrls)
            {
                checkouturl = currenturl + "/checkout";
            }
            else
            {
                checkouturl = currenturl.Replace(".aspx", "/checkout.aspx");
            }

            Response.Redirect(checkouturl);
        }

        protected void cartItemRepeater_ItemDataBound(object source, RepeaterItemEventArgs e)
        {

            System.Web.UI.HtmlControls.HtmlSelect se = (System.Web.UI.HtmlControls.HtmlSelect)e.Item.FindControl("quantity");
            CartItem ci = (CartItem)e.Item.DataItem;
            Literal lttProductName = (Literal)e.Item.FindControl("lttProductName");
            DropDownList ddlQuantity = (DropDownList)e.Item.FindControl("ddlQuantity");
            Literal lttItemTotal = (Literal)e.Item.FindControl("lttItemTotal");

            System.Web.UI.HtmlControls.HtmlInputHidden prodId = (System.Web.UI.HtmlControls.HtmlInputHidden)e.Item.FindControl("productid");
            prodId.Value = ci.ProductId;


            lttProductName.Text = ci.ProductName;
            try
            {
                ddlQuantity.SelectedValue = ci.Quantity.ToString();
            }
            catch
            {
                ddlQuantity.SelectedValue = "20";
            }
            lttItemTotal.Text = Library.FormattedPrice(ci.Quantity * ci.Price);



        }
    }
}